package com.itheima.linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * @FileName SinglyLinkedList
 * @Description
 * @Author Sword
 * @date 2024-11-18
 **/
public class SinglyLinkedListSentinel implements Iterable<Integer> { //整体
    private Node head = new Node(0, null);  //头指针

    /**
     * 迭代器遍历
     *
     * @return
     */
    @Override
    public Iterator<Integer> iterator() {
        //匿名内部类 -> 带名字的内部类
        return new NodeIterator();
    }

    /**
     * 节点类
     */
    private static class Node {
        int value;  //值
        Node next;  //下一个节点指针

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 头插法
     *
     * @param value
     */
    public void addFirst(int value) {
        insert(0, value);
    }


    /**
     * 遍历链表
     *
     * @param consumer
     */
    public void loop1(Consumer<Integer> consumer) {
        Node p = head.next;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    /**
     * for循环遍历
     *
     * @param consumer
     */
    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head.next; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

    private class NodeIterator implements Iterator<Integer> {
        Node p = head.next;

        @Override
        public boolean hasNext() {  //是否有下一个元素
            return p != null;
        }

        @Override
        public Integer next() {     //返回当前值,并返回下一个元素
            int v = p.value;
            p = p.next;
            return v;
        }
    }

    /**
     * 查找尾节点
     *
     * @return
     */
    private Node findLast() {
        Node p;
        for (p = head; p.next != null; p = p.next) {

        }

        return p;
    }

    /**
     * 尾插法
     *
     * @param value
     */
    public void addLast(int value) {
        Node last = findLast();

        last.next = new Node(value, null);
    }


    /**
     * 根据索引查找节点
     *
     * @param index
     * @return
     */
    private Node findNode(int index) {
        int i = -1;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;//没找到

    }


    /**
     * 根据索引查找
     *
     * @param index
     * @return
     */
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw illegalIndex(index);
        }
        return node.value;
    }

    private static IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(String.format("index [%d] 不合法", index));
    }

    /**
     * 向索引位置插入
     *
     * @param index
     * @param value
     */
    public void insert(int index, int value) {
        Node prev = findNode(index - 1);    //找到上一个节点

        if (prev == null) {
            throw illegalIndex(index);//找不到
        }

        prev.next = new Node(value, prev.next);
    }

    /**
     * 删除链表首元素
     */
    public void removeFirst() {

        removeByIndex(0);
    }

    /**
     * 根据索引删除元素
     *
     * @param index
     */
    public void removeByIndex(int index) {
        Node prev = findNode(index - 1);        //找到上一个节点
        if (prev == null) {
            throw illegalIndex(index);
        }
        Node removed = findNode(index);            //被删除的节点
        if (removed == null) {
            throw illegalIndex(index);
        }
        prev.next = removed.next;
    }
}

